package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.entity.CrBidCar;
import com.arong.swing.db.entity.CrCarRecord;
import com.arong.swing.db.entity.CrCheckevent;
import com.arong.swing.db.entity.PagingInfo;
import com.arong.swing.db.entity.PagingResult;
import com.arong.swing.db.entity.SearchCar;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;

public class SearchCarSDAO extends BaseDAO{
    
    private String normal = "正常";
    
    public SearchCarSDAO() {
        this(DB_CHECKRECORD);
    }

    public SearchCarSDAO(String dbFile) {
        super(dbFile);
    }

    @Override
    public String getCreateTableSql() {
        return null;
    }
    
    /**
     * 分页查询
     * @param sql
     * @param pagingInfo
     * @param entity
     * @return 
     * @throws SysException, AppException
     */
    public  PagingResult<SearchCar> queryList(PagingInfo pagingInfo, CrCarRecord car, CrBidCar bidCar,
            CrCheckevent event)
            throws SysException,AppException {
        PagingResult<SearchCar> res = null;
        try {
            res= jdbcUtils.queryList(getSql(car, bidCar, event), SearchCar.class,
                    pagingInfo, getParams(car, bidCar, event));
        } catch (SQLException e) {
            throw new SysException("queryList分页查询 error",e);
        }
        return   res;
    }

    private String getSql(CrCarRecord car, CrBidCar bidCar,
            CrCheckevent event) {
        StringBuffer sql = new StringBuffer("SELECT c.CHECKEVENT_ID,c.ID,c.CP,c.CZSFHM,c.CZXM,c.CLSBDM,c.FDJH,c.CLYS,c.PCSJ,");
        sql.append(" e.USER_ID,e.CHECK_USER,b.CLLB,b.TASK_NAMES "
                + "from CR_CAR_RECORD c left join  CR_CHECKEVENT e on c.CHECKEVENT_ID = e.ID  left join CR_BID_CAR b  on c.ID = b.HCJL_ID "
                + "where 1=1");

        if (StringUtil.isNotBlank(bidCar.getCllb())) {
        	if (normal.equals(bidCar.getCllb())) {
                sql.append(" and b.CLLB is null ");
           }else {
                sql.append(" and b.CLLB is not null ");
            }
        }
        if (StringUtil.isNotBlank(car.getCp())) {
            sql.append(" and c.CP=?");
        }
        if (StringUtil.isNotBlank(car.getClsbdm())) {
            sql.append(" and c.CLSBDM=?");
        }
        if (StringUtil.isNotBlank(car.getFdjh())) {
            sql.append(" and c.FDJH=?");
        }
        if (event.getStartTime() != null) {
            sql.append(" and c.PCSJ>=?");
        }
        if (event.getEndTime() != null) {
            sql.append(" and c.PCSJ <=?");
        }
        if (StringUtil.isNotBlank(event.getUserId())) {
            sql.append(" and e.USER_ID=?");
        }
        
        sql.append(" order by c.PCSJ desc");
        return sql.toString();

    }

    private Object[] getParams(CrCarRecord car, CrBidCar bidCar,
            CrCheckevent event) {
        List<Object> qryList = new ArrayList<Object>();
//        if (StringUtil.isNotBlank(bidCar.getCllb())) {
//            qryList.add(bidCar.getCllb());
//        }
        if (StringUtil.isNotBlank(car.getCp())) {
            qryList.add(car.getCp());
        }
        if (StringUtil.isNotBlank(car.getClsbdm())) {
            qryList.add(car.getClsbdm());
        }
        if (StringUtil.isNotBlank(car.getFdjh())) {
            qryList.add(car.getFdjh());
        }
        if (event.getStartTime() != null) {
            qryList.add(event.getStartTime());
        }
        if (event.getEndTime() != null) {
            qryList.add(event.getEndTime());
        }
        if (StringUtil.isNotBlank(event.getUserId())) {
            qryList.add(event.getUserId());
        }
        return jdbcUtils.setQryList(qryList);
    }
}
